<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once('SimpleDictionary.php');
 
/**
 *
 * Default implementation of IDictionary
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class Dictionary extends SimpleDictionary
{

  /**
   * Filter null values
   */
  const FILTER_NULL = 1;

  /**
   * Filter empty values
   */
  const FILTER_EMPTY = 2;

  /**
   * Constructor.
   *
   * @param mixed $data initial data to populate the Dictionary
   * @return void
   */
  public function __construct($data = null)
  {
    parent::__construct($data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  public function get($name)
  {
    $args = func_get_args();

    $func = array(
      'parent', 'get'
    );

    if (method_exists($this, 'get_' . $name)) {
      $func = array(
        $this, 'get_' . $name
      );

      unset($args[0]);
    }

    return call_user_func_array($func, $args);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#getAll()
   */
  public function getAll()
  {
    $data = $this->data;

    foreach ($this->getGetterNames() as $name) {
      $data[$name] = $this->$name;
    }

    return $data;
  }

  /**
   *
   */
  public function getSetterNames()
  {
    $names = array();

    $methods = get_class_methods($this);

    foreach ($methods as $method) {
      if (strpos($method, 'set_') === 0) {
        $names[] = substr($method, 4);
      }
    }

    return $names;
  }

  /**
   *
   */
  public function getGetterNames()
  {
    $names = array();

    $methods = get_class_methods($this);

    foreach ($methods as $method) {
      if (strpos($method, 'get_') === 0) {
        $names[] = substr($method, 4);
      }
    }

    return $names;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#has($name)
   */
  public function hasGetter($name)
  {
    return method_exists($this, 'get_' . $name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#has($name)
   */
  public function hasSetter($name)
  {
    return method_exists($this, 'set_' . $name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  public function set($name, $value)
  {
    $args = func_get_args();

    $func = array(
      'parent', 'set'
    );

    if (method_exists($this, 'set_' . $name)) {
      $func = array(
        $this, 'set_' . $name
      );

      unset($args[0]);
    }

    return call_user_func_array($func, $args);
  }

  /**
   * Get $this->data
   *
   * @return array
   */
  public function &getData()
  {
    /*$data = $this->getAll();

    foreach ($this->getGetterNames() as $name) {
      $data[$name] = $this->get($name);
    }

    return $data;*/
    return $this->data;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#copyAll($data)
   */
  public function _copyAll($data, $except = null, $filter = null)
  {
    if (empty($data)) {
      return;
    }

    if ($data instanceof IDictionary) {
      $data = $data->getAll();
    }

    $except = ( array ) $except;

    foreach ($data as $name => $value) {
      if (in_array($name, $except))
        continue;

      if ($filter == Dictionary::FILTER_EMPTY && empty($value))
        continue;

      if ($filter == Dictionary::FILTER_NULL && is_null($value))
        continue;

      $this->set($name, $value);
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  protected function _get($name, $default = null, $filter = null)
  {
    if ($this->has($name, $filter)) {
      return parent::_get($name);
    }

    return $default;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#has($name)
   */
  protected function _has($name, $filter = null)
  {
    try {
      $this->_filter($name, $filter);
      $has = true;
    }
    catch (DictionaryFilterException $e) {
      $has = false;
    }

    return $has;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  /*protected function _set($name, $value)
  {
    return parent::_set($name, $value);
  }*/

  protected function _filter($name, $filter = null)
  {
    if (! isset($this->data[$name])) {
      throw new DictionaryFilterException('Value is not set');
    }

    if ($filter == Dictionary::FILTER_NULL && is_null($this->data[$name])) {
      throw new DictionaryFilterException('Value is null');
    }

    if ($filter == Dictionary::FILTER_EMPTY && empty($this->data[$name])) {
      throw new DictionaryFilterException('Value is empty');
    }
  }

}

?>